using System;
using System.Runtime.CompilerServices;
using Discord;
using UnityEngine;

public class Discord_Controller : MonoBehaviour
{
	[Serializable]
	[CompilerGenerated]
	private sealed class _003C_003Ec
	{
		public static readonly _003C_003Ec _003C_003E9 = new _003C_003Ec();

		public static UpdateActivityHandler _003C_003E9__7_0;

		internal void _003CChangeActivity_003Eb__7_0(Result res)
		{
			Debug.Log((object)"Activity Updated!");
		}
	}

	private Discord discord;

	public static Discord_Controller instance;

	private bool isDiscordInitialized;

	private long startTimeUnix = DateTimeOffset.Now.AddHours(-666.0).ToUnixTimeSeconds();

	private void Start()
	{
		instance = this;
		InitializeDiscord();
		if (isDiscordInitialized)
		{
			ChangeActivity((ActivityType)0);
		}
	}

	private void InitializeDiscord()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_002a: Expected O, but got Unknown
		try
		{
			discord = new Discord(1228410356700025012L, 1uL);
			isDiscordInitialized = true;
			Debug.Log((object)"Discord initialized successfully.");
		}
		catch (ResultException ex)
		{
			ResultException ex2 = ex;
			Debug.LogWarning((object)("Failed to initialize Discord: " + ((Exception)(object)ex2).Message));
			Debug.LogWarning((object)"Common reasons are either, discord not being open or no internet connection.");
			isDiscordInitialized = false;
		}
	}

	private void OnDisable()
	{
		if (discord != null)
		{
			discord.Dispose();
			Debug.Log((object)"Discord disposed.");
		}
	}

	public void ChangeActivity(ActivityType Type = (ActivityType)0, string Name = "", string State = "", string Details = "* WE ARE BUSY EXPERIMENTING.", string LargeImage = "hypotheticalicon_default", string LargeText = "DELTARUNE - Hypothesis", string VesselText = "* (A vessel, it looks content.)")
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Expected O, but got Unknown
		if (!isDiscordInitialized)
		{
			Debug.LogWarning((object)"Discord is not initialized, skipping ChangeActivity.");
			return;
		}
		ActivityManager activityManager = discord.GetActivityManager();
		Activity val = default(Activity);
		val = ((!Application.isEditor || DRHDebugManager.instance.DebugModeEnabled) ? new Activity
		{
			Type = Type,
			Name = Name,
			State = State,
			Details = Details,
			Assets = 
			{
				LargeImage = LargeImage
			},
			Assets = 
			{
				LargeText = LargeText
			},
			Assets = 
			{
				SmallImage = "soul"
			},
			Assets = 
			{
				SmallText = VesselText
			},
			Timestamps = 
			{
				Start = startTimeUnix
			}
		} : new Activity
		{
			Type = Type,
			Name = Name,
			State = "[Hypoaster is hard at work]",
			Details = "* LET ME WRITE MORE HYPOTHESES!!",
			Assets = 
			{
				LargeImage = "hypotheticalicon_default"
			},
			Assets = 
			{
				LargeText = "DELTARUNE - Hypothesis"
			},
			Assets = 
			{
				SmallImage = "unity"
			},
			Assets = 
			{
				SmallText = "In unity editor, no leaks! tee-hee!"
			},
			Timestamps = 
			{
				Start = startTimeUnix
			}
		});
		Activity val2 = val;
		object obj = _003C_003Ec._003C_003E9__7_0;
		if (obj == null)
		{
			UpdateActivityHandler val3 = delegate
			{
				Debug.Log((object)"Activity Updated!");
			};
			_003C_003Ec._003C_003E9__7_0 = val3;
			obj = (object)val3;
		}
		activityManager.UpdateActivity(val2, (UpdateActivityHandler)obj);
	}

	private void Update()
	{
		//IL_001e: Expected O, but got Unknown
		if (!isDiscordInitialized || discord == null)
		{
			return;
		}
		try
		{
			discord.RunCallbacks();
		}
		catch (ResultException ex)
		{
			ResultException ex2 = ex;
			Debug.LogWarning((object)("Discord.RunCallbacks() failed: " + ((Exception)(object)ex2).Message));
			Debug.LogWarning((object)"Disabling Discord integration to prevent further issues.");
			isDiscordInitialized = false;
			if (discord != null)
			{
				discord.Dispose();
				discord = null;
			}
		}
	}
}
